﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebStudio.Business.Rule;
using WebStudio.Business.Entity;

public partial class Admin_FrdLinkAdd : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Admin_User"] == null || Session["Admin_User"].ToString() == "")
        {
            Response.Redirect("~/Admin/Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            GetFrdLinkInfo();
        }
    }

    private FriendUrlEntity FrdLinkEntity
    {
        get
        {
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                return null;
            }
            else
            {
                try
                {
                    return FriendUrlRule.GetFriendUrlEntity(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    private void GetFrdLinkInfo()
    {
        FriendUrlEntity entity = new FriendUrlEntity();
        entity = FrdLinkEntity;
        if (entity != null)
        {
            txtSiteName.Text = entity.FriendUrlName;
            txtSiteUrl.Text = entity.FriendUrl;
            chkIsShow.Checked = Convert.ToBoolean(entity.IsVisiable);
            txtMemo.Text = entity.Remark;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string linkname = txtSiteName.Text.Trim();
        string linkurl = txtSiteUrl.Text.Trim();
        if (string.IsNullOrEmpty(linkname))
        {
            return;
        }
        if (string.IsNullOrEmpty(linkurl))
        {
            return;
        }
        FriendUrlEntity frdLinkEntity = new FriendUrlEntity();
        frdLinkEntity.FriendUrlName = linkname;
        frdLinkEntity.FriendUrl = linkurl;
        if (chkIsShow.Checked)
        {
            frdLinkEntity.IsVisiable = 1;
        }
        else
        {
            frdLinkEntity.IsVisiable = 0;
        }
        frdLinkEntity.Remark = txtMemo.Text.Trim();
        frdLinkEntity.LogoPath = "";

        if (FrdLinkEntity == null)
        {
            FriendUrlRule.SaveFriendUrlInfor(frdLinkEntity);
            ApplicationHelper.AddStartupScript(this, "if(confirm('添加成功，是否继续添加？')){location.href='FrdLinkAdd.aspx';}else{location.href='FrdLinkMge.aspx';}");
        }
        else
        {
            frdLinkEntity.FriendUrlId = Convert.ToInt32(Request.QueryString["id"]);
            FriendUrlRule.SaveFriendUrlInfor(frdLinkEntity);
            ApplicationHelper.AddStartupScript(this, "alert('修改成功');location.href='FrdLinkMge.aspx';");
        }
    }
}
